/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
//调用另一个js文件
document.write("<script language='javascript' src='http://api.go2map.com/maps/js/api_v2.5.1.js'></" + "script>");
/**
 * 初始化地图
 */
function initialize()
{
    var map = new sogou.maps.Map(document.getElementById("map_canvas"),{});
//    map.setCenter(new sogou.maps.Point(23522000,3641093),10);//设定中心
return map;
}

/**
 * 根据指定城市区坐标初始化地图
 */
function initialize(xpoint,ypoint)
{
    var myOptions = {
        zoom: 12,
        center: new sogou.maps.Point(xpoint,ypoint)
    }
    var map = new sogou.maps.Map(document.getElementById("map_canvas"),myOptions);
    //       map.setMapTypeId(sogou.maps.MapTypeId.SATELLITE);//将地图设成卫星地图
    return map;
}

/**
 * 创建标注
 */
function marker(xpoint,ypoint){
    var map=initialize(xpoint,ypoint);
    var mk = new sogou.maps.Marker({
        map: map,
        position:map.getCenter()
    });
    var infowindow = new sogou.maps.InfoWindow();
    infowindow.setContent('<b>测试标注</b>');
    sogou.maps.event.addListener(mk, 'click', function() {
        infowindow.open(map, mk);
    }
    );
}

/**
 * 单击地图，获取地图坐标，用坐标绘制标记，移动地图中心到坐标处。
 */
function make_marker(){
    var map=initialize();
    sogou.maps.event.addListener(map, 'click', function(event) {
    //获取点击位置的坐标
//    alert(event.point)
    placeMarker(event.point);
  });

}

/**
 * 根据坐标location创建一个标记
 * 创建一个经纬度坐标var myLatlng = new sogou.maps.LatLng(39.981320,116.320247);
 * 创建一个搜狗坐标var myPoint = new sogou.maps.Point(12956000,4824875);
 */
function placeMarker(location) {
//  var clickedLocation = location;
  var mk = new sogou.maps.Marker({
      position: location,
      map: map,
      title:location+""
  });
//  map.setCenter(location);//移动地图的中心位置
}

/**
 * 传入两个搜狗坐标计算直线距离
 */
function two_point_distans(xpoint1,ypoint1,xpoint2,ypoint2){
    var myLatlng = new sogou.maps.LatLng(xpoint2,ypoint2);
    var myPoint = new sogou.maps.Point(xpoint1,ypoint1);
    var myOptions = {
      zoom: 12,
      center: new sogou.maps.LatLng(39.949945,116.393407),
      mapTypeId: sogou.maps.MapTypeId.ROADMAP
    }
    var map = new sogou.maps.Map(document.getElementById("map_canvas"), myOptions);
    var marker1=new sogou.maps.Marker({
        position: myLatlng,
        map: map,
        title:"经纬度坐标："+myLatlng
//        label:{visible:true,align:"TOP"}
    });
    var marker2=new sogou.maps.Marker({
        position: myPoint,
        map: map,
        title:"搜狗坐标："+myPoint
    });
//        label:{visible:true,align:"BOTTOM"}
    //Convertor只需创建一个唯一的对象即可。
    var convertor=getInstance(sogou.maps.Convertor);
    //标准经纬度转换为Sogou地图坐标
    var sogouCoord=convertor.toSogou(myLatlng);
//    alert("经纬度="+myLatlng+",搜狗坐标="+sogouCoord);
    var bounds=convertor.bounds(sogouCoord,2000);
//    alert("以搜狗坐标="+sogouCoord+"为中心,2000为半径的区域范围="+bounds);
    var distance=convertor.distance(sogouCoord,myPoint);
//    alert(myLatlng+"与"+myPoint+"之间的距离为"+parseInt(distance)+"米");
document.getElementById("form:text1").value=parseInt(distance);
    var line=new sogou.maps.Polyline({
      path: [myLatlng,myPoint],
      strokeColor: "#FF0000",
      strokeOpacity: 1.0,
      strokeWeight: 1,
      title:parseInt(distance)+"米",
      map:map
    });

  }

//获取类的唯一示例
  function getInstance(a) {
    a.hasOwnProperty("_instance")||(a._instance = new a);
    return a._instance
  }

